package com.lantosec.hmrp.modules.supplier.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.supplier.entity.SupplierBad;
import com.lantosec.hmrp.modules.supplier.mapper.SupplierBadMapper;
import com.lantosec.hmrp.modules.supplier.service.ISupplierBadService;
import com.lantosec.hmrp.modules.supplier.service.ISupplierInfoService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierBadVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供应商不良记录 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-21
 */
@Service
@Primary
@Log4j2
public class SupplierBadServiceImpl extends ServiceImpl<SupplierBadMapper, SupplierBad> implements ISupplierBadService {

    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Autowired
    private ISupplierInfoService iSupplierInfoService;

    /**
     * 根据条件查询供应商不良记录
     * @param page
     * @param supplierCode
     * @param badType
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public PageVO<SupplierBadVO> querySupplierBad(PageVO<SupplierBadVO> page, String supplierCode, String badType, String startTime, String endTime) {
        PageVO<SupplierBadVO> pageVO = baseMapper.querySupplierBad(page,supplierCode,badType,startTime,endTime);
        List<SupplierBadVO> list = pageVO.getRecords();
        //查询列表中相对应的字典数名称
        list.forEach(parm->{
            //不良行为类型
            String badType1 = parm.getBadType();
            if(StringUtils.isNotEmpty(badType1)){
                String badTypeName = iSysDictionariesService.getById(badType1).getDictionariesShowName();
                parm.setBadTypeName(badTypeName);
            }
            if(parm.getStopFlag().equals(1)){
                parm.setHandleStateName("暂停采购");
            }
            if(parm.getBlacklistFlag().equals(1)){
                parm.setHandleStateName("纳入企业黑名单");
            }
            //处置状态
            String handleState = parm.getHandleState();
            if(StringUtils.isNotEmpty(handleState) && (!handleState.equals("1") && !handleState.equals("2")) ){
                String handleStateName = iSysDictionariesService.getById(handleState).getDictionariesShowName();
                parm.setHandleStateName(handleStateName);
            }
            //返回不良记录附件
            String uploadIds = parm.getUploadId();
            if(StringUtils.isNotEmpty(uploadIds)){
                String [] s = uploadIds.split(",");
                List list1 = new ArrayList();
                for (String uploadId:s ){
                    try {
                        SysUploads sysUploads = iSysUploadsService.getById(uploadId);
                        String savePath = sysUploads.getSavePath();
                        String fileName = sysUploads.getFileName();
                        Map map = new HashMap();
                        map.put("uploadId",uploadId);
                        map.put("savePath", InitDataListener.uploadsUrl + savePath + fileName);
                        list1.add(map);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                parm.setSavePath(list1);
            }
        });
        pageVO.setRecords(list);
        return pageVO;
    }

    @Override
    public String insertSupplierBad(SupplierBadVO vo) {
        SupplierBad entity = vo.convertToEntity();
        entity.setBadId(UUIDUtils.get());
        //通过供应商编码查询相对应的供应商名称
        String supplierName = iSupplierInfoService.getById(entity.getSupplierCode()).getSupplierName();
        entity.setSupplierName(supplierName);
        entity.setStopFlag(0);
        entity.setBlacklistFlag(0);
        entity.setSubmitFlag(0);
        //获得redis中的用户名和id
        String userId = TokenUtil.getUserId();
        String name = TokenUtil.getName();
        entity.setCreateUserId(userId);
        entity.setCreateUser(name);
        entity.setCreateTime(DateUtils.nowDate());
        this.save(entity);
        return entity.getBadId();
    }

    @Override
    public void managementSupplierBad(List<SupplierBadVO> voList) {
        voList.forEach(parm->{
            parm.setHandleTime(DateUtils.nowDate());
            //获得redis中的用户名和id
            String userId = TokenUtil.getUserId();
            String name = TokenUtil.getName();
            parm.setHandleUserId(userId);
            parm.setHandleUser(name);
            this.updateById(parm.convertToEntity());
        });
    }

    @Override
    public void cancelSupplierPurchase(SupplierBadVO vo) {
        String supplierCode = vo.getSupplierCode();
        Integer stopFlag = vo.getStopFlag();
        Integer blacklistFlag = vo.getBlacklistFlag();
        if(null != stopFlag && stopFlag == 1){
            baseMapper.cancelSupplierPurchase(supplierCode);
        }
        if(null != blacklistFlag && blacklistFlag == 1){
            baseMapper.cancelSupplierBlacklist(supplierCode);
        }
    }
}
